﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OnlineStore.Business_Logic_Layer;
using OnlineStore.Data_Access_Layer;

namespace OnlineStore
{
    public partial class frmOrder : Form
    {
        private BizOrder eBizOrder;
        public frmOrder()
        {
            eBizOrder = new BizOrder();
            InitializeComponent();
        }

        private void btSave_Click(object sender, EventArgs e)
        {
           
            if (eBizOrder.ValidateData(dgOrderDetail.Rows))
            {
                if (cbStatus.Text == "Waiting" || cbStatus.Text == "Waiting for Confirm") // incase review order and update confirm
                {
                    eBizOrder.UpdateOrderStatus("Processing", int.Parse(txtOrderID.Text));
                    eBizOrder.UpdateOrderDetail(int.Parse(txtOrderID.Text), dgOrderDetail.Rows);
                }
                if (cbStatus.Text == "Finished") // in case update finished when customer received product.
                    eBizOrder.UpdateOrderStatus("Finished", int.Parse(txtOrderID.Text));

                tabControl1.SelectedTab = tabView;
                LoadOrderList(cbKey.Text, txtKeyWord.Text);
                ResetValue();
            }
        }
        // Load order information
        private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                LoadOrder();
            }
        }

        private void LoadOrder()
        {
            BizProduct eBizProduct = new BizProduct();

            // clear data in form
            ResetValue();
            try
            {
                // Get order header by id
                tbOrder eOrder = eBizOrder.GetOrderbyKeyWord("ID", txtOrderID.Text).First();
                tbCustomer eCustomer = eBizOrder.GetCustomerbyID(eOrder.Customer_ID);
                // fill data into form.
                txtCustomerName.Text = eCustomer.Cus_Name;
                txtAddress.Text = eCustomer.Address;
                txtPhone.Text = eCustomer.Phone;
                cbStatus.Text = eOrder.Status;
                // Get order detail
                List<tbOrderDetail> eOrderDetailList = eBizOrder.GetOrderDetailbyOrderID(int.Parse(txtOrderID.Text));
                // fill data into datagrid dgOrdertail
                for (int i = 0; i < eOrderDetailList.Count; i++)
                {
                    tbProduct eProduct = eBizProduct.GetProductbyKeyWord("ID", eOrderDetailList[i].Product_ID.ToString()).First();
                    string[] NewRow = new string[] { eOrderDetailList[i].Product_ID.ToString(), eProduct.Product_Name.ToString(), eProduct.Color.ToString(), eProduct.Size.ToString(), eOrderDetailList[i].Qty.ToString(), eOrderDetailList[i].Qty.ToString(), eProduct.Pur_Qty.ToString(), eProduct.Stock_Qty.ToString(), eProduct.Ord_Qty.ToString() };
                    dgOrderDetail.Rows.Add(NewRow);
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void frmOrder_Load(object sender, EventArgs e)
        {
            cbKey.Text = "Status";
            txtKeyWord.Text = "Waiting";
            LoadOrderList(cbKey.Text, txtKeyWord.Text);
        }
        private void LoadOrderList(string strKey, string strValue)
        {
            List<tbOrder> OrderList = eBizOrder.GetOrderbyKeyWord(strKey, strValue);
            tbOrder eOrder = new tbOrder();
            tbCustomer eCustomer = new tbCustomer();
            dgOrderList.Rows.Clear();
            for (int i = 0; i < OrderList.Count; i++)
            {
                eOrder = OrderList.ElementAt<tbOrder>(i);
                eCustomer = eBizOrder.GetCustomerbyID(eOrder.Customer_ID);

                string[] NewRow = new string[] {eOrder.Order_Id.ToString(), eCustomer.Cus_Name, eCustomer.Address, eCustomer.Phone, eOrder.Created_Date.ToString(), eOrder.Status};
                dgOrderList.Rows.Add(NewRow);
            }            
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabAdd;            
            try
            {
                txtOrderID.Text = dgOrderList.Rows[dgOrderList.SelectedCells[0].RowIndex].Cells[0].Value.ToString().Trim();
                LoadOrder();
            }
            catch (Exception ex)
            {
                ex.ToString();

            }
        }
        private void KeyWordCheck(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                LoadOrderList(cbKey.Text, txtKeyWord.Text);
            }
        }

        private void ResetValue()
        {
            dgOrderDetail.Rows.Clear();
            txtAddress.Text = "";
            txtCustomerName.Text = "";
            txtPhone.Text = "";
            //txtOrderID.Text = "";
            cbStatus.Text = "";
        }
    }
}
